// CH6_1.cpp
// by Charles Mc Auley
// "Programming AutoCAD 2000 with ObjectARX"
//
//	This application demonstrates what it takes to load
//	a DCL dialog. You can use this application to test the
//	layout of the DCL dialog. This application does not
//	interact with any of the tiles, its only purpose
//	is to display the dialog, assuming the syntax is
//	correct.
//
/////////////////////////////////////////////////////////


#include <acdb.h>		// acdb definitions
#include <adslib.h>		// ads defs
#include <rxregsvc.h>	// unlock application
#include <aced.h>
#include <dbsymtb.h>	// Symbol Tables
#include <adsdlg.h>		// DCL style dialogs

// TODO: add your own includes

// Function prototypes
// entry point for this application
extern "C" AcRx::AppRetCode acrxEntryPoint( AcRx::AppMsgCode msg, void* );

// helper functions
void initApp  (void);
void unloadApp(void);

// user defined functions
void loaddcl();


/////////////////////////////////////////////////////////////////////
// acrxEntryPoint(internal)
// This function is the entry point for your application.
/////////////////////////////////////////////////////////////////////
AcRx::AppRetCode acrxEntryPoint(AcRx::AppMsgCode msg, void* ptr)
{
	switch (msg) {
		case AcRx::kInitAppMsg :
			acrxUnlockApplication(ptr);
			acrxRegisterAppMDIAware(ptr);
			initApp();
		break;

		case AcRx::kUnloadAppMsg :
			unloadApp();
		break;

		default:
		break;
	} // switch

  return AcRx::kRetOK;
}

void initApp(void)
{
	// TODO: init your application
    // register a command with the AutoCAD command mechanism

    acedRegCmds->addCommand("CH6_APPS", "LOADDCL", "LOADDCL",
                    ACRX_CMD_MODAL, loaddcl);
	acutPrintf("Enter \"LOADDCL\" to view a DCL file.\n");

}

void unloadApp(void)
{
	// TODO: clean up your application
    // Remove the command group added via acedRegCmds->addCommand

	acutPrintf("%s%s", "Goodbye\n", "Removing command group \"CH6_APPS\"\n");
    acedRegCmds->removeGroup("CH6_APPS");

}

// TODO: add your other functions here

void loaddcl()
{
	// Use the acedGetFileD() to select your
	// DCL file.

	int rc; // Return code
	struct resbuf *rbDCL;
	char dclFile[TILE_STR_LIMIT];	// TILE_STR_LIMIT = 255 in adsdlg.h file
	char dclName[80];
	int dclId, dbStat;				// dcl ID and dialog box status
	ads_hdlg dlgHdl;				// Dialog Box handle

	rbDCL = acutNewRb(RTSTR);

	rc = acedGetFileD("Select DCL file to view",	// Dialog Title
					NULL,							// No default name
					"dcl",							// dcl file extension
					2,								// Bit flag disable "Type it" button
					rbDCL);							// result buffer
	
	// Check the return value of rc
	if(rc != RTNORM)
	{
		acutPrintf("\nError selecting DCL file or user pressed Cancel button");
		if(rbDCL != NULL)
		{
			acutRelRb(rbDCL);
		}
		return;
	}

	// resbuf rbDCL now contains the dcl filname and path
	// in its resval.rstring member

	strcpy(dclFile, rbDCL->resval.rstring);

	if(rbDCL != NULL)
	{
		acutRelRb(rbDCL);
	}

	// The name of the dialog is the name in the DCL file that comes
	// before the ":" and the word "dialog" as follows:
	// name : dialog {
	rc = acedGetString(0, "\nDCL dialog name (Case Sensitive) ", dclName);
	if(rc != RTNORM)
	{
		acutPrintf("\nError in retrieving DCL dialog name");
		return;
	}

	// Now that we have the DCL filename, load the dialog box
	rc = ads_load_dialog(dclFile, &dclId);
	if(rc != RTNORM)
	{
		acutPrintf("%s %s", "\nError loading DCL file. ", dclFile);
		return;
	}

	// Now that the dialog is loaded, display it
	rc = ads_new_dialog(dclName, dclId, NULLCB, &dlgHdl);
	if(rc != RTNORM)
	{
		acutPrintf("\nError displaying dialog. ");
		return;
	}

	// Now that the dialog is visible on the screen
	// initialize it.

	rc = ads_start_dialog(dlgHdl, &dbStat);
	if(rc != RTNORM)
	{
		acutPrintf("\nFailed to initialize the dialog box. ");
		return;
	}
	
	// Unload the dialog box
	ads_unload_dialog(dclId);
}
